<?php

require_once 'helper/DbTable.php';

class Department extends DbTable
{
    public static $_TABLE_NAME_ = 'yxb_department';
    public static $required = ['dept_name'];
    public static $members_mapping = ['dept_id', 'dept_info', 'dept_manager_id', 'dept_name'];
    public static $relationship = ['yxb_group'];
    private $dept_id;
    private $dept_info;
    private $dept_manager_id;
    private $dept_name;

    public function __construct($dept_name = "default", $dept_info = null, $dept_manager_id = null)
    {
        $this->setDeptInfo($dept_info);
        $this->setDeptManagerId($dept_manager_id);
        $this->setDeptName($dept_name);
    }

    public static function getTableName()
    {
        return Department::$_TABLE_NAME_;
    }

    public static function getRequired()
    {
        // TODO: Implement getRequired() method.
        return Department::$required;
    }

    public function loadData($data)
    {
        // TODO: Implement loadData() method.
        foreach (Department::$members_mapping as $member) {

            if (array_key_exists($member, $data)) {
                DbTable::callSetMethod($this, $member, $data[$member]);
            } else {
                return false;
            }
        }
        return true;
    }

    public function add()
    {
        // TODO: Implement add() method.
        $format = "insert into %s(%s) values(%s);";
        $attr = [];
        $values = [];
        foreach (Department::$members_mapping as $item) {
            $value = DbTable::callGetMethod($this, $item);
            if ($item == "dept_id" || $value == null)
                continue;
            array_push($attr, $item);
            is_numeric($value) ? array_push($values, "$value") : array_push($values, "\"$value\"");
        }
        $sql = sprintf($format, Department::$_TABLE_NAME_, join(",", $attr), join(",", $values));
        return $sql;
    }

    public function checkRelationship()
    {
        $sql = [];
        foreach (Department::$relationship as $item) {
            $format = "select * from %s where dept_id=%s;";
            array_push($sql, sprintf($format, $item, $this->getDeptId()));
        }
        return $sql;

    }

    public function delete()
    {
        // TODO: Implement delete() method.
        $format = "delete from %s where dept_id=%s;";

        $sql = sprintf($format, Department::$_TABLE_NAME_, $this->dept_id);
        return $sql;
    }

    public function edit($data)
    {
        // TODO: Implement edit() method.
        $format = "update %s set %s where dept_id=%s;";
        $changeSql = [];
        foreach (Department::$members_mapping as $item) {
            if (isset($data[$item]) && $item != "dept_id") {
                DbTable::callsetMethod($this, $item, $data[$item]);
                is_numeric($data[$item]) ? array_push($changeSql, $item . " = " . $data[$item]) : array_push($changeSql, $item . "= \"" . $data[$item] . "\"");;
            }
        }
        $sql = null;
        if (count($changeSql) >= 1)
            $sql = sprintf($format, Department::$_TABLE_NAME_, join(",", $changeSql), $this->getDeptId());
        return $sql;

    }

    public static function query($condition)
    {
        if (isset($condition['name']) && isset($condition['value'])) {

            $format = "select * from %s where %s='%s';";
            $sql = sprintf($format, Department::$_TABLE_NAME_, $condition['name'], $condition['value']);
            return $sql;
        }
        if (isset($condition['page']) && isset($condition['pageSize'])) {
            $page = (int)($condition['page']) - 1;
            $pageSize = (int)($condition['pageSize']);
            if ($page < 0)
                return null;
            $format = "select * from %s limit %d,%d";
            $sql = sprintf($format, Department::$_TABLE_NAME_, $page * $pageSize, $pageSize);
            return $sql;

        }
        return null;
    }

    /**
     * @return mixed
     */
    public function getDeptId()
    {
        return $this->dept_id;
    }

    /**
     * @param mixed $dept_id
     */
    public function setDeptId($dept_id)
    {
        $this->dept_id = $dept_id;
    }

    /**
     * @return mixed
     */
    public function getDeptInfo()
    {
        return $this->dept_info;
    }

    /**
     * @param mixed $dept_info
     */
    public function setDeptInfo($dept_info)
    {
        $this->dept_info = $dept_info;
    }

    /**
     * @return mixed
     */
    public function getDeptManagerId()
    {
        return $this->dept_manager_id;
    }

    /**
     * @param mixed $dept_manager_id
     */
    public function setDeptManagerId($dept_manager_id)
    {
        $this->dept_manager_id = $dept_manager_id;
    }

    /**
     * @return mixed
     */
    public function getDeptName()
    {
        return $this->dept_name;
    }

    /**
     * @param mixed $dept_name
     */
    public function setDeptName($dept_name)
    {
        $this->dept_name = $dept_name;
    }

    public function __toString()
    {
        $data = array();
        foreach (Department::$members_mapping as $item) {
            $key = DbTable::upperFirstName($item,true);
            $value = DbTable::callGetMethod($this, $item);
            $data[$key] = $value;
        }
        return json_encode($data);
    }

    public function copy()
    {
        // TODO: Implement copy() method.
        $temp = new Department();
        foreach (Department::$members_mapping as $item) {
            DbTable::callSetMethod($temp, $item, DbTable::callGetMethod($this, $item));
        }
        return $temp;
    }

    public function changeTo($obj)
    {
        // TODO: Implement changeTo() method.
        foreach (Department::$members_mapping as $item) {
            DbTable::callSetMethod($this, $item, DbTable::callGetMethod($obj, $item));
        }
    }
}